package controlsystem;


	import java.io.IOException;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import managesystem.Student;

	
public class MilestoneServlet extends HttpServlet {
		/*
		 * Deze klasse regelt het verkeer tussen de jsp milestones en de milestoneController. 
		 * @author Christiaan
		 */
	Calendar calendar = Calendar.getInstance();
	
	
	private static final long serialVersionUID = 1L;
		/*
		 * 
		 */
		public void doPost(HttpServletRequest req, HttpServletResponse resp)
	            throws IOException {
			
			
			HttpSession session = req.getSession();
			Student currStud = (Student) session.getAttribute("currStud");

				
				String vak=req.getParameter("vak");
				String description= req.getParameter("description");
				//String type=req.getParameter("pagButton");			
				Date date =	makeDate(req.getParameter("datepicker"));			
				
				
				int sendValue=0;//aantal pag of aantal uur dat men aan milestone wil werken.
				String sendType="geen type";//geeft aan of het over aantal uur of aantal pagina's gaat
				int duur=Integer.valueOf(req.getParameter("duration")).intValue();
				if (duur!=0)
				{sendValue=duur*360000000;
				sendType="Number of hours";}
				int pagina=Integer.valueOf(req.getParameter("pages")).intValue();
			    if (pagina!=0)
			    {sendValue=pagina;
			    sendType="Number of pages";}	

				//Course course=CourseManager.findByName(vak);
		

				CourseDescription coursedesc=CourseManager.findByName(vak);
				Course c = CourseManager.createCourseObj(coursedesc.getId());

				currStud=MilestoneController.makeMilestone(c, description, date, sendType, sendValue, currStud);
				session.setAttribute("currStud", currStud);
				try {
					resp.sendRedirect("index.jsp?id=dashboard");
				} catch (IOException e) {
					
					e.printStackTrace();
				}

				
				
		}
		
		private Date makeDate(String datum)
		
		{  
			int a=(Integer.valueOf(datum.substring(0,2)).intValue());
		    int b=(Integer.valueOf(datum.substring(3,5)).intValue());
		    int c=(Integer.valueOf(datum.substring(6,10)).intValue());
			
			Calendar date = Calendar.getInstance(); 
			date.set(Calendar.MONTH,a-1);
			date.set(Calendar.YEAR,c);
			date.set(Calendar.DAY_OF_MONTH,b);
			Date dat= date.getTime();
			
			
		return dat;
		}
		
		
	
}


